var errinfo = require('../errinfo.js');
var responseJsonUtils = require('../utils/responseJsonUtils.js');

var logout = module.exports = {};

logout.suc = function (req, res, next) {

    var session = req.session;

    if (session && session.id) {
        // session exist
        // delete login-session
        UserSession.remove({
            sessionId: session.id
        }).exec(function (err, userSession) {
            if (err) {
                next(responseJsonUtils.json(errinfo.mongodb.internalError));
                return;
            }
            req.session.user = null;
            res.redirect('/');
        });
    } else {
        res.redirect('/');
    }
};